package com.renren.questions.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.renren.commons.result.ResultResponse;
import com.renren.entity.dto.*;
import com.renren.entity.po.TbError;
import com.renren.entity.po.TbLog;
import com.renren.entity.po.TbTopic;
import com.renren.questions.mapper.TbErrorMapper;
import com.renren.questions.mapper.TbLogMapper;
import com.renren.questions.mapper.TopicDao;

import com.renren.questions.service.TbTopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/topic")
@Api(tags = "题目详情")
public class TopicController {
    @Autowired
    TbTopicService topicService;
    @Autowired
    TopicDao topicDao;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    TbErrorMapper errorDao;
    @Autowired
    TbLogMapper logDao;
    /**
     * 查询共有习题的类型 如第一单元
     */
    @ApiOperation("课程详情")
    @PostMapping("/findElement")
    public ResultResponse findElement(){
        return topicService.findElement();
    }
    /**
     * 查询是第几单元或那个类型的练习题的列表
     */
    @ApiOperation("课程的题目")
    @PostMapping("/findPage")
    public ResultResponse findPage(@RequestBody Topics topics){
        return  topicService.findPage(topics);

    }

    /**
     * 添加题目答案详情
     * @param
     * @return
     */
    @ApiOperation("添加题目")
    @PostMapping("/findTopicAdd")
    public ResultResponse findElementAdd(@RequestBody TbTopic topicElement){
        return topicService.findTopicAdd(topicElement);

    }

    /**
     * 添加单元
     * @param elementAdd
     * @return
     */
    @ApiOperation("添加课程")
    @PostMapping("/findElementAdd")
    public ResultResponse findElementAdd(@RequestBody ElementAdd elementAdd){
        return topicService.findElementAdd(elementAdd);
    }
    /**
     * 修改题目信息
     */
    @ApiOperation("修改题目信息")
    @PostMapping("/findTopicUpdate")
    public ResultResponse findTopicUpdate(@RequestBody TbTopic topicElement){
        return topicService.findTopicUpdate(topicElement);
    }
    /**
     * 题目删除
     */
    @ApiOperation("逻辑删除题目")
    @PostMapping("/findTopicDelete")
    public ResultResponse findTopicDelete(@RequestBody TopicId topicId){
        return topicService.findTopicDelete(topicId);

    }



    @ApiOperation("提交判断答案")
    @PostMapping("/findCompare")
    public ResultResponse findCompare(@RequestBody AnswerList answerList){


        Integer elementId1 = answerList.getElementId();
        QueryWrapper<TbTopic> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(TbTopic::getElementId,elementId1);

        List<TbTopic> topicEntities = topicDao.selectList(
                queryWrapper
        );


        List<ErrorsList> errorsLists=new ArrayList<>();
        List<TopicsList> topicsList = answerList.getTopicsList();
        Integer mistakes=0;
        Integer correct=0;
        Integer grade=0;
        Map map=new HashMap();
        for (TopicsList list : topicsList) {
            for (TbTopic tbTopic : topicEntities) {
                if(tbTopic.getTopicId().equals(list.getTopicId())){
                    if(tbTopic.getTopicCorrect().equals(list.getTopicCorrect())){

                        tbTopic.setAnswer(list.getTopicCorrect());
                        tbTopic.setFlag(true);
                        map.put(tbTopic.getTopicId(), tbTopic);
                        Integer typeId = tbTopic.getTypeId();
                        if(typeId==1){
                            grade+=3;
                        }else if(typeId==2){
                            grade+=2;
                        }else{
                            grade+=2;
                        }
                        correct++;



                    }else{
                        tbTopic.setAnswer(list.getTopicCorrect());
                        tbTopic.setFlag(false);
                        map.put(tbTopic.getTopicId(), tbTopic);


                        TbError errorEntity=new TbError();
                        errorEntity.setTopicId(tbTopic.getTopicId());
                        errorEntity.setTopicName(tbTopic.getTopicName());
                        errorEntity.setTopicCorrect(tbTopic.getTopicCorrect());
                        errorEntity.setPersonName(tbTopic.getPersonName());
                        errorEntity.setCreatTime(new Date());
                        errorEntity.setTypeId(tbTopic.getTypeId());
                        errorEntity.setElementId(tbTopic.getElementId());
                        errorEntity.setOptionA(tbTopic.getOptionA());
                        errorEntity.setOptionB(tbTopic.getOptionB());
                        errorEntity.setOptionC(tbTopic.getOptionC());
                        errorEntity.setOptionD(tbTopic.getOptionD());
                        errorEntity.setOptionE(tbTopic.getOptionE());
                        errorEntity.setOptionF(tbTopic.getOptionF());
                        errorEntity.setDeletes(0);
                        errorEntity.setErrorAnswer(list.getTopicCorrect());
                        errorDao.insert(errorEntity);

                        mistakes++;
                    }
                }
            }
        }

        System.out.println("本次分数为:"+grade);
        TbLog logEntity=new TbLog();
        logEntity.setAnswerName("csh");
        logEntity.setMistakesQuantity(mistakes);
        logEntity.setCorrectQuantity(correct);
        logEntity.setGrossScore(grade);
        logEntity.setCreatTime(new Date());
        logDao.insert(logEntity);


        return ResultResponse.SUCCESS(map);
    }
}
